<?php
namespace App\Services;

use App\Repositories\SmsCodeRespository;
use App\Traits\ReturnTrait;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\SimpleCache\CacheInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Db;

class CommonService
{
    use ReturnTrait;
    /**
     * @Inject
     * @var CacheInterface
     */
    protected $cache;

    public function checkCapture(RequestInterface $request){
        $code = $request->input('capture_code');
        $captcha_id=$request->cookie('captcha');
        if(strtolower($code)==strtolower($this->cache->get($captcha_id))){
            return true;
        }
        return false;
    }

    public function checkSmsCode(RequestInterface $request){
        $mobile = $request->input('mobile');
        $sms_code = $request->input('sms_code');
        $type = $request->input('type');
        $result = SmsCodeRespository::getInstance()->one('*',[[
            'field'=>'mobile',
            'value' => $mobile
        ],[
            'field' => 'type',
            'value' => $type
        ]],[[
            'field' => 'id',
            'sort' => 'desc'
        ]]);
        if(!$result){
            return false;
        }
        if($result->code!=$sms_code){
            return false;
        }
        return true;
    }
}